package com.congee02;

public class SubarraySum_TL {

    public int subarraySum(int[] nums, int k) {

        int n = nums.length;

        long[] prefix = new long[n];
        prefix[0] = nums[0];
        for (int i = 1 ; i < n ; i ++ ) {
            prefix[i] = prefix[i - 1] + nums[i];
        }

        int ans = 0;
        for (int l = 0 ; l < n ; l ++ ) {
            for (int r = l ; r < n ; r ++ ) {
                if (range(prefix, l, r) == k) {
                    ans ++;
                }
            }
        }

        return ans;
    }

    private static long range(long[] prefix, int l, int r) {
        return prefix[r] - (l > 0 ? prefix[l - 1] : 0);
    }

}
